Skip to content

Pre-compile regex patterns in match functions#1691

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/optimize-regex-performance
Open

Pre-compile regex patterns in match functions#1691
Copilot wants to merge 3 commits intomasterfrom
copilot/optimize-regex-performance

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

RegexMatch was compiling regex patterns on every call via regexp.MatchString, causing repeated allocations and compilation overhead.

Changes

Changed RegexMatch to use the existing mustCompileOrGet caching mechanism:

// Before
func RegexMatch(key1 string, key2 string) bool {
    res, err := regexp.MatchString(key2, key1)
    if err != nil {
        panic(err)
    }
    return res
}

// After
func RegexMatch(key1 string, key2 string) bool {
    re := mustCompileOrGet(key2)
    return re.MatchString(key1)
}

Impact

  • 0 B/op - eliminates per-call allocations
  • ~200 ns/op - benchmark shows consistent performance
  • Thread-safe via existing sync.RWMutex in cache
  • Improves KeyMatch2, KeyMatch3, KeyMatch5 (all call RegexMatch)
  • Identical panic behavior on invalid patterns

Added benchmarks to verify performance improvement.

Original prompt

This section details on the original issue you should resolve

<issue_title>[feature] change all regex in xxx match functions to pre-compiled to boost performance</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review January 24, 2026 05:01
Copilot AI and others added 2 commits January 24, 2026 05:04
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
…ment

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Change all regex in match functions to pre-compiled Pre-compile regex patterns in match functions Jan 24, 2026
Copilot AI requested a review from hsluoyz January 24, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] change all regex in xxx match functions to pre-compiled to boost performance

3 participants